const conn = require('../db/db.js')

const showIndexpage = (req,res) => {
    const pagesize = 3
    const nowpage = Number(req.query.page || 1)
    
    const sql = `select * from hm_article 
                LEFT JOIN heima on hm_article.authorId = heima.id
                ORDER BY hm_article.id desc limit ${(nowpage - 1) * pagesize}, ${pagesize};
                select count(*) as count from hm_article`
    conn.query(sql,(err,result) => {
        if(err) return res.render('index.ejs',{islogin:req.session.islogin,user:req.session.user,article:[]})
        const totalpage = Math.ceil(result[1][0].count / pagesize)
        res.render('index.ejs',{
            islogin:req.session.islogin,
            user:req.session.user,
            article:result[0],
            totalpage:totalpage,
            nowpage:nowpage
        })
        
        
    })
    
}
module.exports = {
    showIndexpage
}
